support conditional fields on TypedDict and NamedTuple#2263
support conditional fields on TypedDict and NamedTuple#2263carljm wants to merge 2 commits intopython:mainfrom
Conversation
davidhalter
left a comment
There was a problem hiding this comment.
Is it intentional that this is not specified for Protocol/Enums/Dataclasses? I would probably prefer this as a consistent change for all of these.
However I'm also good with this PR as a first step.
|
Or maybe the question should be: Why is this not defined in a more general way in the spec? Shouldn't this be what we want in all class-like scopes? Conformance tests should probably test this for different scenarios but I personally do not see a reason to add this specifically only to TypedDicts and NamedTuples. This PR is IMO still better then the status quo. |
|
What's special about TypedDict and NamedTuple is that they're very restricted (they don't support class variables, methods, etc.). So it makes sense to enumerate only those things that are explicitly allowed. |
(NamedTuples do support methods, unlike TypedDicts) |
We reached consensus on this a while back in this Discuss thread, but I never got around to actually making the PR.
Seems like in the meantime we accidentally implemented the strict version of the check for TypedDict in ty, but astral-sh/ruff#24702 fixes that. Happy to wait a bit to land this if other type checkers want to have conforming implementations ready, too.